package com.ruoyi.StarStudio.Service.Impl;

import com.ruoyi.StarStudio.Mapper.TreeMapper;
import com.ruoyi.StarStudio.Mapper.ZoneMapper;
import com.ruoyi.StarStudio.Service.TreeService;
import com.ruoyi.StarStudio.Domain.Tree;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

@Service
public class TreeServiceImpl implements TreeService {

    @Autowired
    private TreeMapper treeMapper;

    @Autowired
    private ZoneMapper zoneMapper;

    @Autowired
    protected Validator validator;

    private static final Logger log = LoggerFactory.getLogger(TreeServiceImpl.class);
    @Override
    public List<Tree> getAll() {
        return treeMapper.getAll();
    }

    @Override
    public List<Tree> getList(Tree tree) {
        return treeMapper.getList(tree);
    }

    @Override
    public List<Tree> getAllByZoneId(Integer zone_id) {
        return treeMapper.getAllByZoneId(zone_id);
    }

    @Override
    public Tree getById(Integer tree_id) {
        return treeMapper.getById(tree_id);
    }

    @Override
    public void add(Tree tree) {
        treeMapper.add(tree);
    }

    @Override
    public void update(Tree tree) {
        treeMapper.update(tree);
    }

    @Override
    public void delete(Integer tree_id) {
        treeMapper.delete(tree_id);
    }

    @Override
    public String importTrees(List<Tree> treeList, boolean isUpdateSupport) {
        if (StringUtils.isNull(treeList) || treeList.size() == 0) {
            throw new ServiceException("导入果树数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Tree tree : treeList) {
            try {
                Tree treeByLonAndLat = treeMapper.getByLonAndLat(tree);
                int zoneIdByZoneName = zoneMapper.getZoneIdByZoneName(tree.getZone_name());
                tree.setTree_zoneid(zoneIdByZoneName);
                if (StringUtils.isNull(treeByLonAndLat)) {
                    BeanValidators.validateWithException(validator, tree);
                    treeMapper.add(tree);
                    successNum++;
                    successMsg.append("<br/>" + successNum + ".位置在" + tree.getTree_longitude() +","+ tree.getTree_latitude() + "的果树导入成功");
                } else if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, tree);
                    treeMapper.update(tree);
                    successNum++;
                    successMsg.append("<br/>" + successNum + ".位置在" + tree.getTree_longitude() +","+ tree.getTree_latitude() + "的果树更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + ".位置在" + tree.getTree_longitude() +","+ tree.getTree_latitude() + "的果树已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + ".位置在" + tree.getTree_longitude() +","+ tree.getTree_latitude() + "的果树导入失败: ";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}